package com.ruoyi.erp.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.erp.domain.DictProjectHourModule;
import com.ruoyi.erp.domain.ProjectMember;
import com.ruoyi.erp.service.IProjectMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.ProjectHourModuleMapper;
import com.ruoyi.erp.domain.ProjectHourModule;
import com.ruoyi.erp.service.IProjectHourModuleService;
import com.ruoyi.common.core.text.Convert;

/**
 * 工时模块Service业务层处理
 * 
 * @author sauger
 * @date 2022-04-06
 */
@Service
public class ProjectHourModuleServiceImpl implements IProjectHourModuleService 
{
    @Autowired
    private ProjectHourModuleMapper projectHourModuleMapper;

    @Autowired
    private IProjectMemberService projectMemberService;


    /**
     * 查询工时模块
     * 
     * @param id 工时模块主键
     * @return 工时模块
     */
    @Override
    public ProjectHourModule selectProjectHourModuleById(Integer id)
    {
        return projectHourModuleMapper.selectProjectHourModuleById(id);
    }

    /**
     * 查询工时模块列表
     * 
     * @param projectHourModule 工时模块
     * @return 工时模块
     */
    @Override
    public List<ProjectHourModule> selectProjectHourModuleList(ProjectHourModule projectHourModule)
    {
        return projectHourModuleMapper.selectProjectHourModuleList(projectHourModule);
    }

    /**
     * 新增工时模块
     * 
     * @param projectHourModule 工时模块
     * @return 结果
     */
    @Override
    public int insertProjectHourModule(ProjectHourModule projectHourModule)
    {
        return projectHourModuleMapper.insertProjectHourModule(projectHourModule);
    }

    /**
     * 修改工时模块
     * 
     * @param projectHourModule 工时模块
     * @return 结果
     */
    @Override
    public int updateProjectHourModule(ProjectHourModule projectHourModule)
    {
        return projectHourModuleMapper.updateProjectHourModule(projectHourModule);
    }

    /**
     * 批量删除工时模块
     * 
     * @param ids 需要删除的工时模块主键
     * @return 结果
     */
    @Override
    public int deleteProjectHourModuleByIds(String ids)
    {
        return projectHourModuleMapper.deleteProjectHourModuleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工时模块信息
     * 
     * @param id 工时模块主键
     * @return 结果
     */
    @Override
    public int deleteProjectHourModuleById(Integer id)
    {
        return projectHourModuleMapper.deleteProjectHourModuleById(id);
    }

    /**
     * 根据用户获取module下拉框数据
     *
     * @param userId
     * @param projectFlag normal 普通项目 maintenance 维护项目
     * @return
     */
    @Override
    public List<DictProjectHourModule> selectDictProjectHourModule(Integer userId, String projectFlag) {
        ProjectMember searcheProjectMember = new ProjectMember();
        searcheProjectMember.setUserId(userId);
        searcheProjectMember.getParams().put("projectFlag", projectFlag);
        List<ProjectMember> projectMembers = projectMemberService.selectProjectMemberList(searcheProjectMember);
        List<DictProjectHourModule> list = new ArrayList<>();
        for (ProjectMember projectMember: projectMembers) {
            DictProjectHourModule module = new DictProjectHourModule();
            module.setName(projectMember.getProject().getTitle());
            module.setValue(projectMember.getProjectId().toString());
            module.setSelects(new ArrayList<>());
            ProjectHourModule search = new ProjectHourModule();
            search.setProjectId(projectMember.getProjectId());

            this.selectProjectHourModuleList(search).forEach(m ->{
                DictProjectHourModule module1 = new DictProjectHourModule();
                module1.setName(m.getName());
                module1.setValue(m.getId().toString());
                module.getSelects().add(module1);
            });
            list.add(module);
        }
        return list;
    }
}
